package com.hjlh.aimedical.service.impl;

import com.hjlh.aimedical.common.respone.ResultModel;
import com.hjlh.aimedical.common.utils.CommonUtils;
import com.hjlh.aimedical.dao.UserDao;
import com.hjlh.aimedical.entity.User;
import com.hjlh.aimedical.model.UserModel;
import com.hjlh.aimedical.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserDao userDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel add(User user) {

        try{
            User u = (User) userDao.findByPhone(user.getPhone());
            if(u!=null){
                return ResultModel.fail("用户已存在，请登录",null);
            }
            if(StringUtils.isEmpty(user.getPassword())){
                return ResultModel.fail("密码不能为空",null);
            }
            user.setPassword(CommonUtils.GetMD5Code(user.getPassword()));
            if(StringUtils.isEmpty(user.getPhone())){
                return ResultModel.fail("手机号或邮箱不能为空",null);
            }
            user.setUsercode(UUID.randomUUID().toString());
            userDao.insert(user);
            return ResultModel.success();
        }catch (Exception e) {
            logger.error("UserServiceImpl add",e);
        }
        return ResultModel.fail();
    }

    @Override
    @Transactional
    public User findByPhone(String phone) {
        if(StringUtils.isEmpty(phone)){
            return null;
        }
        User u = userDao.findByPhone(phone);
        return u;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel register(UserModel userModel) {

        try{


            //注册
            if(StringUtils.isEmpty(userModel.getPhone())){
                return ResultModel.fail("账号不能为空",null);
            }
            if(StringUtils.isEmpty(userModel.getPassword())){
                return ResultModel.fail("密码不能为空",null);
            }
            User u = userDao.findByPhone(userModel.getPhone());
            if(u != null){
                return ResultModel.fail("账号已存在，请登录",null);
            }
            User user = new User();
            BeanUtils.copyProperties(userModel,user);
            user.setUsercode(UUID.randomUUID().toString());
            user.setPassword(CommonUtils.GetMD5Code(userModel.getPassword()));
            user.setCreatetime(new Date());
            user.setInvitationcode(userModel.getInvitationcode());
            userDao.insert(user);

            return ResultModel.success("注册成功",null);
        }catch (Exception e){
            logger.error("UserServiceImpl register fail",e);
        }
        return ResultModel.fail("注册失败",null);
    }


}
